package com.hengtiansoft.lfy.utils;

import android.content.Context;
import android.util.Log;

import com.hengtiansoft.lfy.entity.EvaluatorMessageEntity;
import com.hengtiansoft.lfy.entity.EvaluatorModeEntity;
import com.hengtiansoft.lfy.entity.EvaluatorRankings;

import org.apache.http.client.protocol.RequestTargetAuthentication;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.hengtiansoft.lfy.R.string.rankings;

/**
 * 测评Json解析
 */
public class EvaParser {

	private Context appContext;
	private static EvaParser instance = new EvaParser();

	private EvaParser() {
	}

	public static EvaParser getInstance() {
		return instance;
	}

	/**
	 * 解析测评相关信息
	 * @param str
	 * @return
	 */
	public List<EvaluatorModeEntity> parseEvaluatorMode(JSONObject str){

		List<EvaluatorModeEntity> list = new ArrayList<>();
		JSONArray array = str.optJSONArray("data");

		for(int i = 0; i < array.length(); i ++){

			JSONObject jsonObject = array.optJSONObject(i);
			EvaluatorModeEntity modeEntity = new EvaluatorModeEntity();
			modeEntity.setName(jsonObject.optString("paperName"));
			modeEntity.setImagePath(jsonObject.optString("imageUrl"));
			modeEntity.setSerial(""+jsonObject.optInt("pkSerial"));
			list.add(modeEntity);
		}
		return list;
	}

	/**
	 * 解析测评相关信息
	 * @param str
	 * @return
	 */
	public List<EvaluatorMessageEntity> parseEvaluator(JSONObject str){
		List<EvaluatorMessageEntity> list = new ArrayList<>();

			JSONArray array = str.optJSONArray("data");

			for(int i = 0; i < array.length(); i ++){
				JSONObject jsonObject1 = array.optJSONObject(i);
				EvaluatorMessageEntity messageEntity = new EvaluatorMessageEntity();
				messageEntity.setEnglish(jsonObject1.optString("english"));
				messageEntity.setChinese(jsonObject1.optString("chinese"));
				messageEntity.setSerial(""+jsonObject1.optInt("paperSerial"));
				messageEntity.setPkSerial(""+jsonObject1.optInt("pkSerial"));
				messageEntity.setState(0+"");
				list.add(messageEntity);
			}
		return list;
	}
	/**
	 * 解析试题排名
	 * @param str
	 * @return
	 */
	public EvaluatorRankings parseEvaluatorRankings(JSONObject str){

		EvaluatorRankings rankings = new EvaluatorRankings();
		int code = str.optInt("code");

		JSONObject json = str.optJSONObject("data");
		if(json == null)
			return null;
		rankings.setRanking(json.optString("ranking"));

		rankings.setAverageScore(json.optString("averageScore"));
		rankings.setTodaySecs(json.optString("todaySecs"));
		rankings.setTotalSecs(json.optString("totalSecs"));

		rankings.setRetcode(code);

		return rankings;
	}
	/**
	 * 解析练习历史记录
	 * @param str
	 * @return
	 */
	public List<EvaluatorModeEntity> parseExerciseHistory(JSONObject str){
		List<EvaluatorModeEntity> list = new ArrayList<>();
		int code = str.optInt("code");
		JSONArray array = str.optJSONArray("data");
		if(array == null)
			return list;
		for(int i = 0; i < array.length(); i ++){
			EvaluatorModeEntity modeEntity = new EvaluatorModeEntity();
			JSONObject object = array.optJSONObject(i);
			modeEntity.setName(object.optString("paper_name"));
			modeEntity.setImagePath(object.optString("image_url"));
			modeEntity.setSerial(object.optString("pk_serial"));
			modeEntity.setRetcode(code);
			list.add(modeEntity);
		}
		return list;
	}

	/**
	 * 解析已完成内容
	 * @param str
	 * @param ev
     * @return
     */
	public static  List<EvaluatorMessageEntity> parseScoreJson(String str,List<EvaluatorMessageEntity> ev){
		try {
			JSONObject js = new JSONObject(str).optJSONObject("data");
			JSONObject jsonObject = new JSONObject(js.optString("remark"));
			Log.d("tag","jsonObject = "+jsonObject.toString());

			for(int i = 0; i < ev.size(); i ++){
				JSONObject object = jsonObject.getJSONObject(ev.get(i).getPkSerial());
				String examScore = object.optString("examScore");
				ev.get(i).setScore(examScore);
				Log.d("tag","object = "+object.toString());
				JSONArray array = object.optJSONArray("wordsScore");
				String wordScore = "";
				for(int j = 0; j < array.length(); j ++){
					wordScore += array.optString(j);
					if(j < array.length()-1)
						wordScore += " ";
				}
				ev.get(i).setWordscore(wordScore);
				Log.d("tag","wordScore = "+wordScore);
			}
			Log.d("tag","ev = "+ev.toString());
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return ev;
	}
	/**
	 * 生成成绩的JSON
	 * @return
	 */
	public static String getScoreJson(List<EvaluatorMessageEntity> evList){
		JSONObject object = new JSONObject();
		try {
			for(int i = 0;i < evList.size();i ++){
				JSONObject object1 = new JSONObject();
				String scoreString = evList.get(i).getWordscore();
				Log.d("tag","scoreString = "+scoreString);
				String[] str = scoreString.split(" ");
				ArrayList<String> list = new ArrayList<>();
				for(int j = 0; j < str.length; j ++){
					list.add(str[j]);
				}
				JSONArray array = new JSONArray(list);
				object1.put("examScore",evList.get(i).getScore());
				object1.put("wordsScore",array);
//                object1.put("serial",evList.get(i).getPkSerial());
//                object.put(i,object1);
				object.put(evList.get(i).getPkSerial(),object1);
			}
			Log.d("tag","object = "+object.toString());
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return object.toString();
	}
}
